package com.tdkj.action.front.member.buyer;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.shop.BeanGoodFavorite;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.ServiceException;
import com.tdkj.service.shop.IBeanGoodFavoriteService;
import com.tdkj.util.PublicConfing;

/**
 * @author yninfo
 */
public class GoodFavoriteAction extends BaseAction {
	
	private Long id ;
	private Long goodId ;
	
	private List<BeanGoodFavorite> favorites;
	
	private IBeanGoodFavoriteService goodFavoriteService;
	
	public String execute(){
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		
		String name = this.getQueryInfo().get("name");
		String hql = "select bean from " + BeanGoodFavorite.class.getName() + " as bean where bean.user.id='"+userId+"' " ;
		if(name!=null && !"".equals(name)){
			hql = hql + " and bean.good.name like '%"+name+"%' "; 
		}
		hql = hql + " order by createTime desc ";
		Query query = this.createQuery(hql);
		this.queryByPage(query, "goodOrderAction!orderItems.action", 10);
		
		return "success";
	}

	public String add() throws ServiceException{
		BeanUser user  = (BeanUser)ActionContext.getContext().getSession().get(PublicConfing.MEMBER_MEMBWE_OBJ_STRING_CONFIG);
		Long userId = user.getId();
		
		goodFavoriteService.add(userId, goodId);
		
		return execute();
	}
	
	public String remove(){
		goodFavoriteService.remove(id);
		
		return execute();
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the goodId
	 */
	public Long getGoodId() {
		return goodId;
	}

	/**
	 * @param goodId the goodId to set
	 */
	public void setGoodId(Long goodId) {
		this.goodId = goodId;
	}

	/**
	 * @return the favorites
	 */
	public List<BeanGoodFavorite> getFavorites() {
		return favorites;
	}

	/**
	 * @param favorites the favorites to set
	 */
	public void setFavorites(List<BeanGoodFavorite> favorites) {
		this.favorites = favorites;
	}

	/**
	 * @param goodFavoriteService the goodFavoriteService to set
	 */
	@Resource
	public void setGoodFavoriteService(IBeanGoodFavoriteService goodFavoriteService) {
		this.goodFavoriteService = goodFavoriteService;
	}
	
	
	
}
